
#ifndef _HEAP_H
#define _HEAP_H

#include "config.h"
#include <stdint.h>
#include <stddef.h>

// heap table entry types (last 4 bytes)
#define HEAP_BLOCK_TABLE_ENTRY_TAKEN 0x01
#define HEAP_BLOCK_TABLE_ENTRY_FREE 0x00

#define HEAP_BLOCK_HAS_NEXT 0b10000000
#define HEAP_BLOCK_IS_FIRST 0b01000000

typedef unsigned char HEAP_BLOCK_TABLE_ENTRY; 

struct heap_table
{
    HEAP_BLOCK_TABLE_ENTRY* entries; 
    size_t entries_amount; 
}
__attribute__((packed));

struct heap
{
    struct heap_table* table; // pointer to the heap table 
    void* saddr; // heap start address
}
__attribute__((packed));

int heap_create(struct heap* heap, void* ptr, void* end, struct heap_table* table); 
void* heap_malloc(struct heap* heap, const size_t size);
void heap_free(struct heap* heap, void* ptr);

#endif 

